/**
 * Copyright (c) 2015-2020, wss 王山山
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wss.conversion.auto;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.log.JdkLoggerFactory;
import com.jfinal.log.Logger;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.wss.conversion.auto.plugin.AutoConfigC3p0Plugin;
import com.wss.conversion.auto.plugin.AutoConfigDefinePlugin;
import com.wss.conversion.auto.plugin.AutoConfigModelPlugin;
import com.wss.conversion.auto.plugin.AutoConfigPlugin;
import com.wss.utils.ObjectUtil;

/**
 * 根据配置文件和约定命名加载系统需要的内容
 * @author wss
 *
 */
public class ConversionConfig extends JFinalConfig {
	private static final Logger LOG = new JdkLoggerFactory().getLogger(ConversionConfig.class);
	
	@Override
	public void configConstant(Constants me) {
		AutoConfig autoConfig = new AutoConfigConstant(me);
		autoConfig.config();
	}

	@Override
	public void configRoute(Routes me) {
		AutoConfig autoConfig = new AutoConfigRoute(me);
		autoConfig.config();
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置数据库插件
		configDatabasePlugin(me);
		
		// 配置自定义插件
		AutoConfig autoConfig = new AutoConfigDefinePlugin(me);
		autoConfig.config();
	}
	
	/*
	 * 配置与数据库操作相关的插件
	 */
	private void configDatabasePlugin(Plugins me) {
		AutoConfigC3p0Plugin c3p0Config = new AutoConfigC3p0Plugin(me);
		c3p0Config.config();
		C3p0Plugin c3p0Plugin = c3p0Config.getC3p0Plugin();
		if (ObjectUtil.isNotNull(c3p0Plugin)) {
			AutoConfigPlugin modelConfig = new AutoConfigModelPlugin(me, c3p0Config.getC3p0Plugin());
			modelConfig.config();
		}
		else {
			LOG.warn("Database configuration information is not complete，and not starts plugins about model");
		}
	}

	@Override
	public void configInterceptor(Interceptors me) {
		AutoConfig autoConfig = new AutoConfigInterceptor(me);
		autoConfig.config();
	}

	@Override
	public void configHandler(Handlers me) {
		AutoConfig autoConfig = new AutoConfigHandler(me);
		autoConfig.config();
	}
}
